

//工具类使用abstract定义时,写代码时不会提示创建实例选项
import 'package:flutter/material.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:xsdk_flutter/xsdk/framework/base/xsdk_function.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

abstract class RBWidgets_SelfService{

  static Widget getTitleBarWidget(
      XPageState pageState,
      {
        String? title,
        bool isTitleAlignmentCenter=true,
        Color backgroundColor=Colors.white,
        String? rightBt,
        GestureTapCallback? onTapRightBt,
        GestureTapCallback? onTapLeftBack,
        Widget? rightWidget,
        bool isShowSearchBar=false,
        bool isShowLeftBt=true,
        String? initSearchKey,
        String? hintSearchWord,
        ValueChanged<String>? onChangedSearchBar,
        ValueChanged<String>? onSubmitSearchBar,
      }
      ){

    if(title!=null){
      if(title.length>10){
        title='${title.substring(0,10)}...';
      }
    }

    Widget leftBackWidget=isShowLeftBt?XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getWidgetText(
            '返回',
            style: XStyleUtils.getTextStyle(
                fontSize: 40,
                color: backgroundColor==Colors.white?RBColors.reabamColor4:Colors.white
            ),
            padding:XWidgetUtils.getEdgeInsets(60, 0, 60, 0)
        ),
        onTap: onTapLeftBack??(){
          pageState.finishPage();
        }
    ):XWidgetUtils.getLayoutContainer();

    return XWidgetUtils.getLayoutContainer(
        height: 140,
        width: double.infinity,
        //margin: XWidgetUtils.getEdgeInsets(0, XTempData.statusBarHeight, 0, 0),
        decoration: XWidgetUtils.getBoxDecoration(
          border: Border(bottom: BorderSide(color: XColorUtils.dividerColor,width: XTempData.defaultDividerHeight)),
          backgroundColor: backgroundColor,
        ),
        child:isShowSearchBar?XWidgetUtils.getLayoutRow(
            children: [
              leftBackWidget,
              XWidgetUtils.getLayoutExpanded(
                  XWidgetUtils.getXLayoutRoundedRect(
                      radius_topLeft: 12,
                      radius_topRight: 12,
                      radius_bottomRight: 12,
                      radius_bottomLeft: 12,
                      padding: XWidgetUtils.getEdgeInsets(30, 0, 30, 0),
                      margin: XWidgetUtils.getEdgeInsets(26, 0, 70, 0),
                      alignment: Alignment.center,
                      height: 80,
                      backgroundColor: Color(0xFFEEF0F3),
                      child: XWidgetUtils.getWidgetTextField(
                        hintText: hintSearchWord,
                        hintTextStyle: XStyleUtils.getTextStyle(
                          fontSize:34,
                          color: Color(0xFF6E7790),
                        ),
                        style:XStyleUtils.getTextStyle(
                          fontSize:34,
                          color: XColorUtils.color333333,
                        ),
                        initialValue: initSearchKey,
                        onChanged: onChangedSearchBar,
                        onSubmitted: onSubmitSearchBar,
                      )
                  )
              )
            ]
        ):
        XWidgetUtils.getLayoutStack(
            alignment:isTitleAlignmentCenter?AlignmentDirectional.center:AlignmentDirectional.centerStart,
            children: [
              XStringUtils.isEmpty(title)?XWidgetUtils.getLayoutContainer():
              XWidgetUtils.getWidgetText(
                title!,
                style: XStyleUtils.getTextStyle(
                    fontSize: 46,
                    color: backgroundColor==Colors.white?XColorUtils.color333333:Colors.white,
                    fontWeight: FontWeight.bold
                ),
                padding: XWidgetUtils.getEdgeInsets(isTitleAlignmentCenter?0:60, 0, 0, 0),
              ),
              XWidgetUtils.getLayoutPositioned(
                  left: 0,
                  child: leftBackWidget
              ),
              XWidgetUtils.getLayoutPositioned(
                  right: 60,
                  child:  rightWidget ?? (XStringUtils.isEmpty(rightBt)?XWidgetUtils.getLayoutContainer():
                  XWidgetUtils.getWidgetGestureDetector(
                      child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                          height: 80,
                          padding: XWidgetUtils.getEdgeInsets(23, 1, 23, 1),
                          backgroundColor: backgroundColor,
                          borderWidth: 2,
                          borderColor: backgroundColor==Colors.white?RBColors.reabamColor4:Colors.white,
                          alignment: Alignment.center,
                          child: XWidgetUtils.getWidgetText(
                            rightBt!,
                            style: XStyleUtils.getTextStyle(
                              fontSize: 34,
                              color: backgroundColor==Colors.white?RBColors.reabamColor4:Colors.white,
                            ),
                            textAlign:TextAlign.center,
                          )
                      ),
                      onTap: onTapRightBt
                  ))
              ),
            ]
        )
    );
  }

  // 获取新增界面选择的Item
  static Widget getCommonInfoTextWidget({
    required String leftTextString,
    TextStyle? leftTextStyle,
    String? rightTextValue,
    TextStyle? rightTextStyle,
    String rightTextHintText = "请选择",
    TextStyle? rightTexHintStyle,
    bool isRequired = false,
    bool isShowRightArrow = true,
    bool isShowBottomDivider = true,
    bool isShowTopDivider = false,
    GestureTapCallback? onTap, //单击
  }) {
    List<Widget> rightWidgets = [];

    rightWidgets
        .add(XWidgetUtils.getLayoutExpanded(isShowRightArrow && (rightTextValue == null || rightTextValue.isEmpty)
        ? XWidgetUtils.getWidgetText(
        rightTextHintText,
        style: rightTexHintStyle??XStyleUtils.getTextStyle(
          fontSize: 34,
          color: XColorUtils.color999999,
          lineHeight: 1,
        ),
        textAlign: TextAlign.right
    ): XWidgetUtils.getWidgetText(
      rightTextValue ?? "",
      style: rightTextStyle??XStyleUtils.getTextStyle(
        fontSize: 34,
        color: RBColors.reabamColor4,
        lineHeight: 1,
      ),
      textAlign: TextAlign.right,
      maxLines: 1,
    )));

    if (isShowRightArrow) {
      rightWidgets.add(const SizedBox(
        width: 10,
        height: 10,
      ));
      rightWidgets.add(XImageUtils.getImageWidgetWithAsset('common_right.png', width: 16, height: 28));
    }

    return XWidgetUtils.getWidgetGestureDetector(
        onTap: onTap,
        child: getBaseInfoLeftRightWidget(
          leftTextString: leftTextString,
          leftTextStyle: leftTextStyle,
          isRequired: isRequired,
          rightWidget: XWidgetUtils.getLayoutRow(children: rightWidgets),
          isShowBottomDivider: isShowBottomDivider,
          isShowTopDivider: isShowTopDivider,
        ));
  }

  // 新增界面中左右样式的布局
  static Widget getBaseInfoLeftRightWidget({
    required String leftTextString,
    TextStyle? leftTextStyle,
    double? leftWidgetWidth,
    required Widget rightWidget,
    bool isRequired = false,
    bool isShowBottomDivider = true,
    bool isShowTopDivider = false,
    double? height = 120,
    CrossAxisAlignment rowCrossAxisAlignment = CrossAxisAlignment.center,
  }) {
    List<Widget> widgets = [];

    widgets.add(XWidgetUtils.getLayoutContainer(
        width: leftWidgetWidth,
        child: XWidgetUtils.getLayoutRow(
            mainAxisSize: MainAxisSize.min,
            children: [
              XWidgetUtils.getWidgetText(
                  leftTextString,
                  style: leftTextStyle??XStyleUtils.getTextStyle(
                    fontSize: 34,
                    color: XColorUtils.color333333,
                    lineHeight: 1,
                  )
              ),
              isRequired ? getRequiredWidget() : XWidgetUtils.getLayoutContainer(),
            ])));

    widgets.add(const SizedBox(width: 10));
    widgets.add(XWidgetUtils.getLayoutExpanded(rightWidget));

    return XWidgetUtils.getLayoutContainer(
        height: height,
        margin: XWidgetUtils.getEdgeInsets(30, 0, 30, 0),
        padding: XWidgetUtils.getEdgeInsets(30, 0, 30, 0),
        decoration: XWidgetUtils.getBoxDecoration(
            backgroundColor: Colors.white,
            border: (isShowBottomDivider || isShowTopDivider)
                ? Border(
                top: isShowTopDivider
                    ?  BorderSide(color: XColorUtils.dividerColor, width: XTempData.defaultDividerHeight)
                    : BorderSide.none,
                bottom: isShowBottomDivider
                    ?  BorderSide(color: XColorUtils.dividerColor, width: XTempData.defaultDividerHeight)
                    : BorderSide.none)
                : null
        ),
        child: Row(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: rowCrossAxisAlignment,
            children: widgets
        )
    );
  }

  //搜索条
  static Widget getSearchBarWidget({
    required XFunctionResultCallback<String>? searchResultCallback,
    required XFunctionWidgetOnClick<String?>? onTapSearch,
    String? initSearchValue, //初始值
    bool autofocus=false,
    String? hintText = '请输入搜索内容',
    int rightCustomWidgetType = 0, //自定义右侧图标, 0:不显示, 1:高级筛选图标, 2:商品分类图标, 3:扫描图标
    List<Widget>? rightCustomWidgets,
    bool isShowLeftBackIcon = false,
    GestureTapCallback? onTapRightCustomIcon,
    Color outSideBackgroundColor = Colors.white,
    Color? searchBarBackgroundColor,
    Color iconColor = XColorUtils.colorCCCCCC,
    Color textColor = XColorUtils.color333333,
    Color hintTextColor = XColorUtils.colorCCCCCC,
    EdgeInsets? margin,
  }) {
    //自定义右侧图标
    List<Widget> outsideRightCustomWidgets = [];
    if (rightCustomWidgetType == 1) {
      //高级筛选图标
      outsideRightCustomWidgets.add(XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getLayoutPadding(
            padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0),
            child: XImageUtils.getImageWidgetWithAsset(
              'shaixuan_5.png',
              width: 24,
              height: 24,
              color: iconColor,
            )),
        onTap: onTapRightCustomIcon,
      ));
    } else if (rightCustomWidgetType == 2) {
      //商品分类图标
      outsideRightCustomWidgets.add(XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getLayoutPadding(
            padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0),
            child: XImageUtils.getImageWidgetWithAsset(
              'fenlei_black.png',
              width: 24,
              height: 24,
              color: iconColor,
            )),
        onTap: onTapRightCustomIcon,
      ));
    } else if (rightCustomWidgetType == 3) {
      //扫描图标
      outsideRightCustomWidgets.add(XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getLayoutPadding(
            padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0),
            child: XImageUtils.getImageWidgetWithAsset(
              'saoyisao_er.png',
              width: 24,
              height: 24,
              color: iconColor,
            )),
        onTap: onTapRightCustomIcon,
      ));
    } else if (rightCustomWidgets != null && rightCustomWidgets.isNotEmpty) {
      // 自定义图标
      outsideRightCustomWidgets.addAll(rightCustomWidgets);
    }

    return XStatefulWidget(() => XComponentSearchBarState(
      initSearchValue: initSearchValue,
      searchResultCallback: searchResultCallback,
      isShowLeftBackIcon: isShowLeftBackIcon,
      hintText: hintText,
      outsideRightCustomWidgets: outsideRightCustomWidgets,
      outSideBackgroundColor: outSideBackgroundColor,
      searchBarBackgroundColor: searchBarBackgroundColor,
      iconColor: iconColor,
      textColor: textColor,
      hintTextColor: hintTextColor,
      onTapSearch: onTapSearch,
      margin: margin,
      autofocus:autofocus,
    ));
  }

  // 获取必填按钮
  static Widget getRequiredWidget() {
    return XWidgetUtils.getWidgetText("*", style: XStyleUtils.getTextStyle(fontSize: 34, color: const Color(0xFFE61121)));
  }


  static Widget getBottomBarOneButton({
    String text = '确定',
    required VoidCallback onPressed,
  }) {
    return XWidgetUtils.getLayoutContainer(
        height: 260,
        width: double.infinity,
        backgroundColor: Colors.white,
        padding: XWidgetUtils.getEdgeInsets(90, 0, 90, 0),
        alignment: Alignment.center,
        decoration: XWidgetUtils.getBoxDecoration(border: Border(top: BorderSide(color: XColorUtils.dividerColor,width: XTempData.defaultDividerHeight))),
        child: getButton(
            text: text,
            onPressed: onPressed)
    );
  }

  /*
  按钮,支持圆角,背景,边框
   */
  static Widget getButton({
    required String text,
    double fontSize = 46,
    Color fontColor = Colors.white,
    required VoidCallback onPressed,
    VoidCallback? onLongPress,
    double? width, //注意不能为double.infinity
    EdgeInsets? margin, //getEdgeInsets(left,top,right,bottom)
    EdgeInsets? padding, //getEdgeInsets(left,top,right,bottom)
    Color? backgroundColor, //按钮背景颜色
    Color? borderColor, //边框颜色
    double? borderWidth,
  }) {
    backgroundColor ??= RBColors.reabamColor4;
    borderWidth??=XTempData.defaultDividerHeight;

    return XWidgetUtils.getXButtonRoundedRectAngle(
      text: text,
      fontSize: fontSize,
      fontColor: fontColor,
      onPressed: onPressed,
      onLongPress: onLongPress,
      width: width,
      margin: margin,
      padding: padding,
      backgroundColor: backgroundColor,
      borderColor: borderColor,
      borderWidth: borderWidth,
    );
  }


  //+,-
  static Widget getCountChangeWidget(
      {
        required String text,
        required XFunctionWidgetOnClick onClick,
        EdgeInsets? margin,
        EdgeInsets? padding,
      }
      ){
    return XWidgetUtils.getLayoutContainer(
        height: 60,
        alignment: Alignment.center,
        margin: margin,
        padding: padding,
        child: XWidgetUtils.getLayoutRow(
            mainAxisSize:MainAxisSize.min,
            children: [
              XWidgetUtils.getWidgetGestureDetector(
                  child: XWidgetUtils.getXLayoutRoundedRect(
                    radius_bottomLeft: 6,
                    radius_topLeft: 6,
                    radius_bottomRight: 0,
                    radius_topRight: 0,
                    width: 60,
                    height: 60,
                    alignment: Alignment.center,
                    backgroundColor: RBColors.reabamColor4,
                    child: XImageUtils.getImageWidgetWithAsset(
                        'ss_jian.png',
                      width: 60,
                      height: 60
                    )
                  ),
                  onTap: (){
                    onClick('-');
                  }
              ),
              XWidgetUtils.getLayoutContainer(
                  width: 107,
                  height: 60,
                  alignment: Alignment.center,
                  backgroundColor: Colors.white,
                  decoration: XWidgetUtils.getBoxDecoration(
                      border: Border.all(color:RBColors.reabamColor4, style: BorderStyle.solid, width:XTempData.defaultDividerHeight)
                  ),
                  child: XWidgetUtils.getWidgetText(
                      text,
                      style: XStyleUtils.getTextStyle(
                          fontSize: 38,
                          fontWeight: FontWeight.bold,
                          color: XColorUtils.color000000
                      )
                  )
              ),
              XWidgetUtils.getWidgetGestureDetector(
                  child: XWidgetUtils.getXLayoutRoundedRect(
                    radius_bottomLeft: 0,
                    radius_topLeft: 0,
                    radius_bottomRight: 6,
                    radius_topRight: 6,
                    width: 60,
                    height: 60,
                    alignment: Alignment.center,
                    backgroundColor: RBColors.reabamColor4,
                      child: XImageUtils.getImageWidgetWithAsset(
                          'ss_jia.png',
                          width: 60,
                          height: 60
                      )
                  ),
                  onTap: (){
                    onClick('+');
                  }
              ),
            ]
        )
    );
  }


}
